<?php

namespace App\Http\Requests;

use App\Models\PmCert;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class PmCertRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     *
     * @todo 项目ID等还未对齐
     */
    public function rules(): array
    {
        return [
            'title' => 'required|string|min:1|max:64',
            'lx_cert_template_id' => [
                'required',
                'string',
                Rule::unique(PmCert::class, 'lx_cert_template_id')->ignore($this->id)->whereNull('deleted_at'),
            ],
            'lx_cert_view_url' => 'required|string|min:1|max:200',
        ];
    }

    /**
     * 获取验证错误的自定义属性
     *
     * @return string[]
     */
    public function attributes()
    {
        return [
            'title' => '证书模版名称',
            'lx_cert_template_id' => '灵析证书模版ID',
            'lx_cert_view_url' => '灵析证书模版预览地址',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    public function messages()
    {
        return [
            'title' => '证书模版名称不能为空',
            'lx_cert_template_id' => '灵析证书模版ID不能为空或已存在',
            'lx_cert_view_url' => '灵析证书模版预览地址不能为空',
        ];
    }
}
